package com.winning.jsyd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winning.jsyd.dao.UDDao;
import com.winning.jsyd.entity.business.*;
import com.winning.jsyd.entity.describe.*;
import com.winning.jsyd.entity.enums.ServiceEnum;
import com.winning.jsyd.service.UDService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author z_dd@winning.com.cn
 * @date 2019/5/5 16:46
 */
@Slf4j
@Service
public class UDServiceImpl implements UDService {

    @Autowired
    UDDao udDao;

    @Value("${jsyd.auth.userCode:320903000000}")
    private String userCode; //  医院编码

    @Value("${jsyd.auth.unitsCode:320903000000}")
    private String unitsCode; //  操作员号

    /**
     * 居民健康信息
     *
     * @param page
     * @param serviceEnum,
     * @param date
     * @return
     */
    @Override
    public ResultSet saveHaPersonInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<HaPersonInfoEntity> ipage = udDao.saveHaPersonInfo(page, serviceEnum.getTableName(), date);

        DataList<HaPersonInfoEntity> haPersonInfoEntityDataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size());
        haPersonInfoEntityDataList.setData(ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(haPersonInfoEntityDataList));
    }

    /**
     * 高血压报告卡
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return ResultSet<DmHighbloodReport>
     * @author z_dd@winning.com.cn
     * @date 2019/5/29 10:47
     */
    @Override
    public ResultSet saveHighbloodReport(Page page, ServiceEnum serviceEnum, String date) {
        IPage<DmHighbloodReport> ipage = udDao.saveHighbloodReport(page, serviceEnum.getTableName(), date);
        List<DataList> dataLists = new ArrayList();

        for (DmHighbloodReport record : ipage.getRecords()) {
            DataList dataList = new DataList(serviceEnum.getType(), 1L);
            dataList.setData(Arrays.asList(record.getBasicInfo(), record));
            dataLists.add(dataList);
        }
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 糖尿病报告卡
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/5/30 10:07
     */
    @Override
    public ResultSet saveDiabetesReport(Page page, ServiceEnum serviceEnum, String date) {
        IPage<DmDiabetesReport> ipage = udDao.saveDiabetesReport(page, serviceEnum.getTableName(), date);
        List<DataList> dataLists = new ArrayList();

        for (DmDiabetesReport record : ipage.getRecords()) {
            DataList dataList = new DataList(serviceEnum.getType(), 1L);
            dataList.setData(Arrays.asList(record.getBasicInfo(), record));
            dataLists.add(dataList);
        }
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 高血压访视信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/5/30 16:30
     */
    @Override
    public ResultSet saveHighbloodVisit(Page page, ServiceEnum serviceEnum, String date) {
        IPage<DmHighbloodVisit> ipage = udDao.saveHighbloodVisit(page, serviceEnum.getTableName(), date);

        List<DataList> dataLists = new ArrayList();
        for (DmHighbloodVisit record : ipage.getRecords()) {
            DataList dataList = new DataList(serviceEnum.getType(), 1L);
            dataList.setData(Arrays.asList(record.getcVisit(), record));
            dataLists.add(dataList);
        }
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 糖尿病访视信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/5/31 13:15
     */
    @Override
    public ResultSet saveDiabetesVisit(Page page, ServiceEnum serviceEnum, String date) {

        IPage<DmDiabetesVisit> ipage = udDao.saveDiabetesVisit(page, serviceEnum.getTableName(), date);
        List<DataList> dataLists = new ArrayList();
        for (DmDiabetesVisit record : ipage.getRecords()) {
            DataList dataList = new DataList(serviceEnum.getType(), 1L);
            dataList.setData(Arrays.asList(record.getcVisit(), record));
            dataLists.add(dataList);
        }
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 门诊收费信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/5 9:41
     */
    @Override
    public ResultSet saveOcmsRcptInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssRcptMaster> ipage = udDao.saveOcmsRcptInfo(page, serviceEnum.getTableName(), date);

        List<DataList> dataLists = new ArrayList();
        MssRcptMaster mssRcptMaster = ipage.getRecords().get(0);
        DataList<MssRcptMaster> dataList1 = new DataList<>(serviceEnum.getType(), 1L);
        dataList1.setData(Arrays.asList(mssRcptMaster));
        dataLists.add(dataList1);

        List<MssRcptBill> mssRcptBills = udDao.selectMssRcptBill(mssRcptMaster.getUnitsCode(), mssRcptMaster.getRcptNo());
        DataList<MssRcptBill> dataList2 = new DataList<>("1403", (long) mssRcptBills.size());
        dataList2.setData(mssRcptBills);
        dataLists.add(dataList2);
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 门诊就诊病历信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/10 10:10
     */
    @Override
    public ResultSet saveMssOutInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssOutDiagnosis> ipage = udDao.saveMssOutInfo(page, serviceEnum.getTableName(), date);

        MssOutDiagnosis mssOutDiagnosis = ipage.getRecords().get(0);
        List<DataList> dataLists = new ArrayList();

        DataList<MssOutVisit> dataList1 = new DataList<>(serviceEnum.getType(), 1L, Arrays.asList(mssOutDiagnosis.getVisit()));
        dataLists.add(dataList1);

        DataList<MssOutDiagnosis> dataList2 = new DataList<>("1602", 1L, Arrays.asList(mssOutDiagnosis));
        dataLists.add(dataList2);
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 门诊处方主记录和明细记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/11 10:35
     */
    @Override
    public ResultSet saveMssPrescInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssPrescMaster> ipage = udDao.saveMssPrescInfo(page, serviceEnum.getTableName(), date);

        MssPrescMaster mssPrescMaster = ipage.getRecords().get(0);
        List<DataList> dataLists = new ArrayList();

        DataList<MssPrescMaster> dataList1 = new DataList<>(serviceEnum.getType(), 1L, Arrays.asList(mssPrescMaster));
        dataLists.add(dataList1);

        DataList<MssPrescDetail> dataList2 = new DataList<>("1605", 1L, Arrays.asList(mssPrescMaster.getDetail()));
        dataLists.add(dataList2);
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 检查主记录和明细记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/12 13:55
     */
    @Override
    public ResultSet saveEmrExamInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssExamMaster> ipage = udDao.saveEmrExamInfo(page, serviceEnum.getTableName(), date);

        MssExamMaster mssExamMaster = ipage.getRecords().get(0);
        List<DataList> dataLists = new ArrayList();

        DataList<MssExamMaster> dataList1 = new DataList<>(serviceEnum.getType(), 1L, Arrays.asList(mssExamMaster));
        dataLists.add(dataList1);

        DataList<MssExamDetail> dataList2 = new DataList<>("1607", 1L, Arrays.asList(mssExamMaster.getDetail()));
        dataLists.add(dataList2);
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 检验主记录和明细记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/12 16:50
     */
    @Override
    public ResultSet saveEmrTestInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssTestMaster> ipage = udDao.saveEmrTestInfo(page, serviceEnum.getTableName(), date);

        MssTestMaster mssTestMaster = ipage.getRecords().get(0);
        List<DataList> dataLists = new ArrayList();

        DataList<MssTestMaster> dataList1 = new DataList<>(serviceEnum.getType(), 1L, Arrays.asList(mssTestMaster));
        dataLists.add(dataList1);

        List<MssTestDetail> list = udDao.selectMssTestDetail(mssTestMaster.getUnitsCode(), mssTestMaster.getTestName());
        DataList<MssTestDetail> dataList2 = new DataList<>("1609", (long) list.size(), list);
        dataLists.add(dataList2);
        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);

    }

    /**
     * 住院病人费用明细信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return com.winning.jsyd.entity.describe.ResultSet
     * @author z_dd@winning.com.cn
     * @date 2019/6/13 14:15
     */
    @Override
    public ResultSet saveInpPatsBillDetail(Page page, ServiceEnum serviceEnum, String date) {

        IPage<MssPatsBillDetail> ipage = udDao.saveInpPatsBillDetail(page, serviceEnum.getTableName(), date);

        DataList<MssPatsBillDetail> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 病人结算主记录和明细信息
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveInpPatsSettle(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssPatsSettleMaster> ipage = udDao.saveInpPatsSettle(page, serviceEnum.getTableName(), date);

        DataList<MssPatsSettleMaster> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 医嘱记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveMssInOrder(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssPatsSettleMaster> ipage = udDao.saveMssInOrder(page, serviceEnum.getTableName(), date);

        DataList<MssPatsSettleMaster> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 住院处方主记录和明细记录。
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveEmrPrescInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<HMssPrescMaster> ipage = udDao.saveEmrPrescInfo(page, serviceEnum.getTableName(), date);

        HMssPrescMaster mssTestMaster = ipage.getRecords().get(0);
        List<DataList> dataLists = new ArrayList();

        DataList<HMssPrescMaster> dataList1 = new DataList<>(serviceEnum.getType(), 1L, Arrays.asList(mssTestMaster));
        dataLists.add(dataList1);

        DataList<HMssPrescDetail> dataList2 = new DataList<>("1775", 1L, Arrays.asList(mssTestMaster.getDetail()));
        dataLists.add(dataList2);

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), dataLists);
    }

    /**
     * 入院记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveMssAdminInfo(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssResidentAdmin> ipage = udDao.saveMssAdminInfo(page, serviceEnum.getTableName(), date);

        DataList<MssResidentAdmin> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 手术申请
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveMssOperationApply(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssOperationApply> ipage = udDao.saveMssOperationApply(page, serviceEnum.getTableName(), date);

        DataList<MssOperationApply> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 手术记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveMssOperationRecord(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssOperationRecord> ipage = udDao.saveMssOperationRecord(page, serviceEnum.getTableName(), date);

        DataList<MssOperationRecord> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 出院记录
     *
     * @param page
     * @param serviceEnum
     * @param date
     * @return
     */
    @Override
    public ResultSet saveMssDischarge(Page page, ServiceEnum serviceEnum, String date) {
        IPage<MssDischarge> ipage = udDao.saveMssDischarge(page, serviceEnum.getTableName(), date);

        DataList<MssDischarge> dataList = new DataList<>(serviceEnum.getType(), (long) ipage.getRecords().size(), ipage.getRecords());

        return new ResultSet(ipage.getPages(), ipage.getTotal(), ipage.getSize(), ipage.getCurrent(), Arrays.asList(dataList));
    }

    /**
     * 更新上传时间
     *
     * @param tableName
     * @param date
     */
    @Override
    public void insertDate(String tableName, String date) {
        udDao.updateTimestamp(tableName, date);
    }

    @Override
    public void insertErrorLog(String taskID, String tableName, List<String> errorMsgs) {
        if (CollectionUtils.isNotEmpty(errorMsgs)) {
            udDao.insertErrorLog(taskID, tableName, errorMsgs);
        }
    }

    @Override
    public void statisticalResults(Record record) {
        udDao.statisticalResults(record);
    }


}
